package Managers 
{
	import Character2.Character;
	import Character2.ICharacter;
	import flash.display.MovieClip;
	import flash.net.SharedObject;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	/**
	 * ...
	 * @author Jonathan Chouinard
	 */
	public class GameInfo
	{
		public static var _saveGameDictionnary:Dictionary = new Dictionary();
		public static var _characters:Dictionary = new Dictionary();
		public static var _flagsArray:Dictionary = new Dictionary();
		private static var _saveGame:SharedObject;
		public static var _instance:GameInfo;
		public static function Instanciate():GameInfo
		{
			if (_instance == null)
			{
				_instance = new GameInfo(new SingletonBlocker());
			}
			return _instance;
		}
		
		public function GameInfo(blocker:SingletonBlocker = null):void
		{
			if (blocker == null)
			{
				throw new Error("You can't new this singleton. Please use GameInfo.instance() to use the functions");
			}
		}
		
		public function SaveGame(sender:String):void
		{
			var player:ICharacter = new Character();
			var byte:ByteArray = new ByteArray();
			_characters["player1"] = player;
			byte.writeObject(new <Dictionary>[_characters]);
			byte.compress();
			_flagsArray["Test"] = false;
			_saveGame = SharedObject.getLocal("SaveGame");
			_saveGameDictionnary[sender] = new Dictionary();
			(_saveGameDictionnary[sender] as Dictionary)["Characters"] = byte;
			(_saveGameDictionnary[sender] as Dictionary)["Flags"] = _flagsArray;
			_saveGame.data.SaveGame = _saveGameDictionnary;
			_saveGame.flush();
		}
		
		public function LoadGame(sender:String):void
		{
			_saveGame = SharedObject.getLocal("SaveGame");
			if (_saveGame.data.SaveGame != null)
			{
				_saveGameDictionnary = _saveGame.data.SaveGame;
				if (_saveGameDictionnary[sender] != null)
				{
					var tempDict:Dictionary = _saveGameDictionnary[sender];
					var byte:ByteArray = tempDict["Characters"];
					byte.uncompress();
					byte.position = 0;
					var test:Vector.<Dictionary> = byte.readObject() as Vector.<Dictionary>;
					_characters = test[0];
					_flagsArray = tempDict["Flags"];
				}
			}
		}
	}
}
class SingletonBlocker{public function SingletonBlocker(){}}


